﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace template_dll
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        public SqlConnection conn;
        public int actionId = 0;
        public DateTime date1 = DateTime.Now.Date;
        public DateTime date2 = DateTime.Now.Date;

        private void Form5_Load(object sender, EventArgs e)
        {
            try
            {
                if (actionId == 0)
                {
                    panel1.Visible = false;
                    dataGridView1.DataSource = new SqlStorProc().SqlProcExecWithQueryOneTable(conn, "pas_Actions_FactorsSel", 300, new SqlStorProc.sp_params[0]).DefaultView;
                    foreach (DataGridViewColumn col in dataGridView1.Columns)
                    {
                        col.ReadOnly = true;
                    }
                }
                else
                {
                    SqlStorProc.sp_params[] par = new SqlStorProc.sp_params[3];
                    par[0].name = "@actid";
                    par[0].value = actionId;
                    par[1].name = "@date1";
                    par[1].value = date1;
                    par[2].name = "@date2";
                    par[2].value = date2;
                    DataTable tbl = new SqlStorProc().SqlProcExecWithQueryOneTable(conn, "pas_Actions_FactorsSel", 300, par);
                    foreach (DataColumn c in tbl.Columns) c.ReadOnly = false;
                    dataGridView1.DataSource = tbl.DefaultView;
                    foreach (DataGridViewColumn col in dataGridView1.Columns)
                    {
                        if (col.Index == 1)
                        {
                            col.ReadOnly = false;
                            col.DefaultCellStyle.BackColor = Color.Coral;
                        }
                        else col.ReadOnly = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Сохранить внесенные изменения для текущей акции?", "Сохранить изменения?", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    SqlStorProc.sp_params[] par = new SqlStorProc.sp_params[3];
                    par[0].name = "@actionid";
                    par[0].value = actionId;
                    par[1].name = "@month";
                    par[2].name = "@factor";

                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        par[1].value = row.Cells[0].Value;
                        par[2].value = row.Cells[1].Value;
                        new SqlStorProc().SqlProcExecNonQuery(conn, "pas_Actions_FactorsIns", 30, par);
                    }
                    MessageBox.Show("Все данные сохранены");
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
